package dashboard.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import dashboard.utils.FileUtil;

/**
 * 
 * @author xu guoneng
 *
 */
public class DebugDbCommandService extends DatabaseService{
	
	public DebugDbCommandService(){
		SIMPLE_SQL_RESULT_OUT_FILE = "debug.html";
	}
	
	/**
	 * To generate HTML results based on the output of SqlPlus
	 * @param command
	 * @return
	 */
	public String debugUserInputCommandHtml(String command){
		String fileName = "temp_debug_" + new java.util.Date().getTime() + ".sql";
		
		System.out.println("Input command: " + command);
		
		command = command.trim();
		if(command.length() == 0){
			return "No input found.";
		}
		else{
			String formattedcommand = "set linesize 9999\r\n"
					+ "set pagesize 50000\r\n"
					+ "set verify on\r\n"
					+ "set echo on\r\n"
					+ "set feedback on\r\n"
					+ "set trimspool on\r\n"
					+ "set termout on\r\n"
					+ "SET MARKUP HTML ON\r\n"
					+ "spool " + getResultOutputFilePath() + "\r\n"
				    + command;
			
			if(command.charAt(command.length()-1) != ';'){
				formattedcommand += "\r\n/ ";
			}
				    
			formattedcommand +=  "\r\n EXIT\r\n/ ";
	
			FileUtil.writeFile(fileName, formattedcommand);
			String result = executeSqlScript(FileUtil.FILE_DIR + fileName);
			FileUtil.deleteFile(FileUtil.FILE_DIR + fileName);
			
			return result;
		}
	}
}
